/*This is the game javascript for ee-ut-battleship created by Marko Tiidla and Priit Tiidla*/

this.Game = function(playerOne, playerTwo, GameId){
	this.playerOne = playerOne;
	this.playerTwo = playerTwo;
	this.GameId = GameId;
};

this.GAMES = new Array();

this.sunkInRowForSound = new Array();



/**
 * When enemy board is clicked, shoot(id) is sent to server, to get response
 */
this.enemyClick = function() {
	if(playerTurn){
		var result = shoot(this.id);
		if (result == 0) //when the player misses
		{
			playerTurn = false;
			$("#player").removeClass("turn");
			$("#opponent").addClass("turn");

				
			if (! $(this).hasClass("miss") && ! $(this).hasClass("hit") && ! $(this).hasClass("sunk")){
				hitEnemyBoard(this, enemyBoard, result); //hit the enemy board, where it was clicked 
				
				
				
//				if (enemyShot != "-10"){
//					var hit = hitBoard(enemyShot, fullBoard, SHIPLIST);
//				}
//				alert("tere");
//				while (hit==1){
//					enemyShot = getEnemyShot();
//					hit = hitBoard(enemyShot, fullBoard, SHIPLIST);
//				};
			}
		} else if (result == -1) //when the player clicks on a square already marked "miss", "hit" or "sunk"
		{
			
			playerTurn = true;
			$("#player").addClass("turn");
			$("#opponent").removeClass("turn");
				

		} else //mark the enemy board hit or sunk
		{
			hitEnemyBoard(this, enemyBoard, result);
			playerTurn = true;
			$("#player").addClass("turn");
			$("#opponent").removeClass("turn");
	

		}
	}
	
};



/**
 * This function marks player game board according to enemy shot
 * @param square cell where enemy shot
 * @param board game board
 * @param shiplist list of ships
 * @returns true when enemy hit your board, false if didn't
 */

this.hitBoard = function(square, board, shiplist) {
	
	var victory = false;
	if (square.length == 5){
		victory = true;
		square = square.substring(0,2);
	} else if (square.length == 6){
		victory = true;
		square = square.substring(0,3);
	}
	
	var result = 0;
	historyHitList.push("1");
	opponentHistoryList.push("-");
	playerHistoryList.push(square);
	
	
	var hit = false;
	for (i in shiplist) {
		var ship = shiplist[i];
		for (it in ship.itself) {
			if (ship.itself[it].id == square) {
				for (j in board) {
					if (board[j].id == square) {

						// if ship is already hit, append hitlist
						if (jQuery.inArray(board[j], ship.hitlist) == -1) {
							
							if (ship.id == "ship2") result = 2;
							else if (ship.id == "ship22") result = 3;
							else if (ship.id == "ship23") result = 4;
							else if (ship.id == "ship2vert") result = 5;
							else if (ship.id == "ship2vert2") result = 6;
							else if (ship.id == "ship2vert3") result = 7;
							else if (ship.id == "ship3") result = 9;
							else if (ship.id == "ship32") result = 9;
							else if (ship.id == "ship3vert") result = 10;
							else if (ship.id == "ship3vert2") result = 11;
							else if (ship.id == "ship4") result = 12;
							else if (ship.id == "ship4vert") result = 13;
							
							ship.hitlist.push(board[j]);
							$(board[j]).addClass("hit");
							
							//Play this sound when shot hits
							if (mute == false){
								var snd = new Audio("resources/Hit.wav");
								snd.play();
							}
							
							hit = true;
							//if ship is sunk remove hit and add sunk
							if (ship.itself.length == ship.hitlist.length) {
								
								if (ship.id == "ship1" || ship.id == "ship12"
									 || ship.id == "ship13" || ship.id == "ship14") result = 1;
								else if (ship.id == "ship2") result = 14;
								else if (ship.id == "ship22") result = 15;
								else if (ship.id == "ship23") result = 16;
								else if (ship.id == "ship2vert") result = 17;
								else if (ship.id == "ship2vert2") result = 18;
								else if (ship.id == "ship2vert3") result = 19;
								else if (ship.id == "ship3") result = 20;
								else if (ship.id == "ship32") result = 21;
								else if (ship.id == "ship3vert") result = 22;
								else if (ship.id == "ship3vert2") result = 23;
								else if (ship.id == "ship4") result = 24;
								else if (ship.id == "ship4vert") result = 25;
								
								for (h in ship.hitlist) {
									$(ship.hitlist[h]).removeClass("hit");
									$(ship.hitlist[h]).addClass("sunk");
								}
								if (mute == false){
								//Play this sound when ship sinks
									var snd = new Audio("resources/Explosion.wav");
									snd.play();
									
								}
							}
							break;
						}
					}
				}
			}
		}
	}
	opponentHistoryResultList.push("-1");
	if(victory) {
		result += 100;
	}
	playerHistoryResultList.push(result);
	
	if (victory){
		return 2;
	}
	if (!hit) {
		//sunkInRowForSound = new Array();
		for (j in board) {
			if (board[j].id == square) {
				if (!$(board[j]).hasClass("hit")
						&& !$(board[j]).hasClass("sunk")) {
					$(board[j]).addClass("miss");
					
					if (mute == false){
					//Play this sound when shot misses
						var snd = new Audio("resources/Splash.wav");
						snd.play();
					}
					
					break;
				}
			}
		}
		return 0;
	} else {
		return 1;
	}
};
/**
 * Mark enemyboard according to player click
 * @param square clicked square
 * @param board game board
 * @param result type of hit
 */
function hitEnemyBoard(square, board, result) {
	var win = false;
	opponentHistoryList.push(square.id);
	playerHistoryList.push("-");
	playerHistoryResultList.push("-1");
	opponentHistoryResultList.push(result);
	historyHitList.push("0");
	
	for (i in board) {
		if (board[i].id == square.id) {
			if (result>100){
				win = true;
				
				result -= 100;
			}
			if (result == 0) {
				$(board[i]).addClass("miss");
				
				sunkInRowForSound = new Array();
				if (mute == false){
				//Play this sound when shot misses
					var snd = new Audio("resources/Splash.wav");
					snd.play();
				}
			} else if (result == 1){
				$(board[i]).addClass("sunk");
				//Play this sound when ship sinks
				sunkInRowForSound.push(1);
				if (mute == false){
					
					chooseSound(sunkInRowForSound);
				}
			} else if (result > 1 && result <= 13) {
				
				result-=2;
				enemyShipsHit[result].push(board[i]); //when ship was hit store hit values to know when ship will be sunk
				$(board[i]).addClass("hit");
				
				//Play this sound when shot hits
				if (mute == false){
					var snd = new Audio("resources/Hit.wav");
					snd.play();
					
				}
				
				
				
			} else if (result > 13) {
				
				result-=14;
				enemyShipsHit[result].push(board[i]);
				sunkInRowForSound.push(1);
				//Play this sound when ship sinks
				if (mute == false){
					chooseSound(sunkInRowForSound);
					
				}
				for (j in enemyShipsHit[result]) {

					for (k in board) {
						if (enemyShipsHit[result][j].id == board[k].id) {
							$(board[k]).addClass("sunk");
						}
					}
				}
			}
		}
		
		}
	if (win == true){
		victory();	
	}
	

}
/**
 * Notify player about victory, disable enemy board clicks, and at the moment refresh the page when clicked on "Tagasi"
 */
function victory() {
	saveGameToLocalStorage();
	gameIsActive = false;
	$().toastmessage('showNoticeToast', 'Palju &otilde;nne!');
	$('.enemy').attr('onclick','').unbind('click');
	document.getElementById("newgame").innerHTML = "Tagasi";
	$('#newgame').click(function() {location.reload(true);});

}

this.loss = function() {
	saveGameToLocalStorage();
	gameIsActive = false;
	$().toastmessage('showNoticeToast', 'Kaotasid lahingu');
	$('.enemy').attr('onclick','').unbind('click');
	document.getElementById("newgame").innerHTML = "Tagasi";
	$('#newgame').click(function() {location.reload(true);});

};

function chooseSound(list){
	
	if(list.length < 2){
		var snd = new Audio("resources/Explosion.wav");
		snd.play();
	}

	if(list.length == 2){
		var snd = new Audio("resources/Double_Kill.wav");
		snd.play();
	}
	if(list.length == 3){
		var snd = new Audio("resources/Killing_Spree.wav");
		snd.play();
	}
	
	if(list.length > 3){
		var random = Math.floor((Math.random()*3)+1);
		var playFile;
		if (random == 1){
			playFile = "resources/GodLike.wav";
		}
		if (random == 2){
			playFile = "resources/monster_kill.wav";
		}
		if (random == 3){
			playFile = "resources/Unstoppable.wav";
		}
		
		var snd = new Audio(playFile);
		snd.play();
		
	}
	
	
}
